//package com.zdp.distributedemo.service;
//
//
//import com.zdp.distributedemo.mapper.OrderItemMapper;
//import com.zdp.distributedemo.mapper.OrderMapper;
//import com.zdp.distributedemo.mapper.ProductMapper;
//import com.zdp.distributedemo.model.Order;
//import com.zdp.distributedemo.model.OrderItem;
//import com.zdp.distributedemo.model.Product;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.PlatformTransactionManager;
//import org.springframework.transaction.TransactionDefinition;
//import org.springframework.transaction.TransactionStatus;
//
//import javax.annotation.Resource;
//import java.math.BigDecimal;
//import java.util.Date;
//
///**
// * @author sesshomaru
// * @date 2020/10/9 19:40
// *
// *
// */
//@Service
//@Slf4j
//public class 同步代码块 {
//
//    @Resource
//    private OrderMapper orderMapper;
//    @Resource
//    private OrderItemMapper orderItemMapper;
//    @Resource
//    private ProductMapper productMapper;
//    //购买商品id
//    private int purchaseProductId = 100100;
//    //购买商品数量
//    private int purchaseProductNum = 1;
//    // 手动控制事务
//    @Autowired
//    private PlatformTransactionManager platformTransactionManager;
//    @Autowired
//    private TransactionDefinition transactionDefinition;
//
//    // 因为OrderService 是单例的所以 object 也是单例的可以当同步代码块的对象锁。
//    // 对象锁和类锁synchronized (OrderService.class)  ，如果是对象是多例的那么仍然会存在超卖的现象，类锁锁的是类不管对象是不是单例都只会允许
//    // 同一时刻只有一个线程进入同步代码块
//    private Object object = new Object();
//
//    // @Transactional(rollbackFor = Exception.class)
//    public Integer createOrder() throws Exception{
//        Product product = null;
//        synchronized (this) {
//            // 进入方法是首先获取事务
//            TransactionStatus transactionStatus = platformTransactionManager.getTransaction(transactionDefinition);
//
//            product = productMapper.selectByPrimaryKey(purchaseProductId);
//            if (product==null){
//                // 异常时事务回滚
//                platformTransactionManager.rollback(transactionStatus);
//                throw new Exception("购买商品："+purchaseProductId+"不存在");
//            }
//            //商品当前库存
//            Integer currentCount = product.getCount();
//            log.info("当前线程:{},库存数:{}", Thread.currentThread().getName(), currentCount);
//
//            //校验库存
//            if (purchaseProductNum > currentCount){
//                // 异常时事务回滚
//                platformTransactionManager.rollback(transactionStatus);
//                throw new Exception("商品"+purchaseProductId+"仅剩"+currentCount+"件，无法购买");
//            }
//            productMapper.updateProductCount(purchaseProductNum, new Date(), product.getId());
//
//            // 开启就一定要提交
//            platformTransactionManager.commit(transactionStatus);
//        }
//            // 再次获取事务
//            TransactionStatus transactionStatus = platformTransactionManager.getTransaction(transactionDefinition);
//            Order order = new Order();
//            order.setOrderAmount(product.getPrice().multiply(new BigDecimal(purchaseProductNum)));
//            order.setOrderStatus(1);//待处理
//            order.setReceiverName("xxx");
//            order.setReceiverMobile("13311112222");
//            order.setCreateTime(new Date());
//            order.setCreateUser("xxx");
//            order.setUpdateTime(new Date());
//            order.setUpdateUser("xxx");
//            orderMapper.insertSelective(order);
//
//            OrderItem orderItem = new OrderItem();
//            orderItem.setOrderId(order.getId());
//            orderItem.setProductId(product.getId());
//            orderItem.setPurchasePrice(product.getPrice());
//            orderItem.setPurchaseNum(purchaseProductNum);
//            orderItem.setCreateTime(new Date());
//            orderItem.setUpdateTime(new Date());
//            orderItemMapper.insertSelective(orderItem);
//
//            // 方法最后手动的提交事务
//            platformTransactionManager.commit(transactionStatus);
//
//            return order.getId();
//        }
//
//}
//
//
